import 'package:EZ_Batter/src/tool/assets.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../tool/model.dart';

class DetailList extends StatefulWidget {
  const DetailList({super.key});

  @override
  State<DetailList> createState() => _DetailListState();
}

class _DetailListState extends State<DetailList> {
  final GlobalController modelController = Get.put(GlobalController());

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(builder: (BuildContext context, BoxConstraints constriants) {
      double w_item = (constriants.maxWidth - 30) / 3;
      return Obx(() {
        var model = modelController.defaultModel.value;
        final List<Map<String, String>> list = [
          {'Current': '${model.v <= 0 ? '--' : model.a}A'},
          {'Voltage': '${model.v <= 0 ? '--' : model.v}V'},
          {'Status': model.v <= 0 ? '--' : model.status},
          {'Temperature': model.v <= 0 ? '--℃/--°F' : ('${model.average_dc}℃/${model.average_df}°F')},
          {'Power': '${model.v <= 0 ? '--' : model.power}W'},
          {'Average voltage': '${model.v <= 0 ? '--' : model.average_v}V'}
        ];
        return GridView.builder(
            itemCount: list.length,
            physics: const NeverScrollableScrollPhysics(),
            padding: const EdgeInsets.all(10),
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                crossAxisCount: 3,
                crossAxisSpacing: 10,
                mainAxisSpacing: 10,
                childAspectRatio: (constriants.maxWidth - 30) / 1.5 / (constriants.maxHeight - 30)),
            itemBuilder: (BuildContext context, index) {
              return Container(
                padding: const EdgeInsets.only(left: 5),
                decoration: BoxDecoration(borderRadius: BorderRadius.circular(4), color: Color_theme),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(list[index].keys.first,
                        textAlign: TextAlign.left,
                        style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w200, color: Colors.white)),
                    SizedBox(
                      width: w_item, // 限制宽度
                      child: Align(
                        alignment: Alignment.centerLeft,
                        child: FittedBox(
                          fit: BoxFit.scaleDown, // 让文本缩小以适应
                          child: Text(
                            list[index].values.first,
                            textAlign: TextAlign.left,
                            maxLines: 1, // 只允许单行
                            style: const TextStyle(fontSize: 16, fontWeight: FontWeight.normal, color: Colors.white),
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              );
            });
      });
    });
  }
}
